package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationEight;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class YinDeficiency extends Activity implements View.OnClickListener {
    private Button next9,whetherYinDeficiency;
    private RadioGroup warmHandsAndFeet,feverOnBodyAndFace,
                    dryLips,lipstick,
                    constipate,twoZygomaticFlushing,
                    dryEyes,dryMouth;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yindeficiency);
        //绑定控件资源
        next9=findViewById(R.id.Next9);

              warmHandsAndFeet=findViewById(R.id.WarmHandsAndFeet);
              feverOnBodyAndFace=findViewById(R.id.FeverOnBodyAndFace);
              dryLips=findViewById(R.id.DryLips);
              lipstick=findViewById(R.id.Lipstick);
              constipate=findViewById(R.id.Constipate);
              twoZygomaticFlushing=findViewById(R.id.TwoZygomaticFlushing);
              dryEyes=findViewById(R.id.DryEyes);
              dryMouth=findViewById(R.id.DryMouth);

        //设置监听器
        next9.setOnClickListener(this);

                warmHandsAndFeet.setOnClickListener(this);
                feverOnBodyAndFace.setOnClickListener(this);
                dryLips.setOnClickListener(this);
                lipstick.setOnClickListener(this);
                constipate.setOnClickListener(this);
                twoZygomaticFlushing.setOnClickListener(this);
                dryEyes.setOnClickListener(this);
                dryMouth.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Next9:{

                //计算结果,把结果写入缓存
                YinDeficiencyResult();
                Toast.makeText(YinDeficiency.this, "体质问卷结束,正在匹配相应医生", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(YinDeficiency.this,FinalAndFindDoctor.class));

            }
            break;

        }

    }
    public void YinDeficiencyResult(){
        int warmHandsAndFeetid=   warmHandsAndFeet.getCheckedRadioButtonId();
        RadioButton warmHandsAndFeetbtn= findViewById(warmHandsAndFeetid);
        String warmHandsAndFeetfin=warmHandsAndFeetbtn.getText().toString();

        int  feverOnBodyAndFaceid= feverOnBodyAndFace.getCheckedRadioButtonId();
        RadioButton feverOnBodyAndFacebtn= findViewById(feverOnBodyAndFaceid);
        String feverOnBodyAndFacefin=feverOnBodyAndFacebtn.getText().toString();

        int  dryLipsid=  dryLips.getCheckedRadioButtonId();
        RadioButton  dryLipsbtn= findViewById( dryLipsid);
        String   dryLipsfin=  dryLipsbtn.getText().toString();

        int lipstickid= lipstick.getCheckedRadioButtonId();
        RadioButton lipstickbtn= findViewById(lipstickid);
        String lipstickfin=lipstickbtn.getText().toString();

        int constipateid=constipate.getCheckedRadioButtonId();
        RadioButton constipatebtn= findViewById(constipateid);
        String constipatefin=constipatebtn.getText().toString();


        int twoZygomaticFlushingid= twoZygomaticFlushing.getCheckedRadioButtonId();
        RadioButton twoZygomaticFlushingbtn= findViewById(twoZygomaticFlushingid);
        String twoZygomaticFlushingfin=twoZygomaticFlushingbtn.getText().toString();

        int dryEyesid= dryEyes.getCheckedRadioButtonId();
        RadioButton dryEyesbtn= findViewById(dryEyesid);
        String dryEyesfin=dryEyesbtn.getText().toString();

        int dryMouthid=  dryMouth.getCheckedRadioButtonId();
        RadioButton dryMouthbtn= findViewById(dryMouthid);
        String dryMouthfin=dryMouthbtn.getText().toString();




        Map<String,Integer>  warmHandsAndFeetMap=new HashMap<String,Integer>();
        warmHandsAndFeetMap.put("没有",1);
        warmHandsAndFeetMap.put("很少",2);
        warmHandsAndFeetMap.put("有时",3);
        warmHandsAndFeetMap.put("常常",4);
        warmHandsAndFeetMap.put("经常",5);

        Map<String,Integer> feverOnBodyAndFaceMap=new HashMap<String,Integer>();
        feverOnBodyAndFaceMap.put("没有",1);
        feverOnBodyAndFaceMap.put("很少",2);
        feverOnBodyAndFaceMap.put("有时",3);
        feverOnBodyAndFaceMap.put("常常",4);
        feverOnBodyAndFaceMap.put("经常",5);

        Map<String,Integer>   dryLipsMap=new HashMap<String,Integer>();
        dryLipsMap.put("没有",1);
        dryLipsMap.put("很少",2);
        dryLipsMap.put("有时",3);
        dryLipsMap.put("常常",4);
        dryLipsMap.put("经常",5);

        Map<String,Integer>  lipstickMap=new HashMap<String,Integer>();
        lipstickMap.put("没有",1);
        lipstickMap.put("很少",2);
        lipstickMap.put("有时",3);
        lipstickMap.put("常常",4);
        lipstickMap.put("经常",5);

        Map<String,Integer> constipateMap=new HashMap<String,Integer>();
        constipateMap.put("没有",1);
        constipateMap.put("很少",2);
        constipateMap.put("有时",3);
        constipateMap.put("常常",4);
        constipateMap.put("经常",5);

        Map<String,Integer> twoZygomaticFlushingMap=new HashMap<String,Integer>();
        twoZygomaticFlushingMap.put("没有",1);
        twoZygomaticFlushingMap.put("很少",2);
        twoZygomaticFlushingMap.put("有时",3);
        twoZygomaticFlushingMap.put("常常",4);
        twoZygomaticFlushingMap.put("经常",5);

        Map<String,Integer> dryEyesMap=new HashMap<String,Integer>();
        dryEyesMap.put("没有",1);
        dryEyesMap.put("很少",2);
        dryEyesMap.put("有时",3);
        dryEyesMap.put("常常",4);
        dryEyesMap.put("经常",5);

        Map<String,Integer> dryMouthMap=new HashMap<String,Integer>();
        dryMouthMap.put("没有",1);
        dryMouthMap.put("很少",2);
        dryMouthMap.put("有时",3);
        dryMouthMap.put("常常",4);
        dryMouthMap.put("经常",5);





        Integer yinDResult =warmHandsAndFeetMap.get(warmHandsAndFeetfin)+
                feverOnBodyAndFaceMap.get(feverOnBodyAndFacefin) +
                dryLipsMap.get(  dryLipsfin) +
                lipstickMap.get(  lipstickfin)+
                constipateMap.get(  constipatefin)+
                twoZygomaticFlushingMap.get( twoZygomaticFlushingfin)+
                dryEyesMap.get( dryEyesfin)+
                dryMouthMap.get(dryMouthfin)
                ;
        ScoreCalculationEight scoreCalculationEight=new ScoreCalculationEight(yinDResult);
        String ConversionScore=scoreCalculationEight.getResult();
        TextView yinDeficiencyResult=(TextView)findViewById(R.id.YinDeficiencyResult);
        yinDeficiencyResult.setText("阴虚质结果:"+ConversionScore);

        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }
    }
}
